<?php

/**
 * @name: HOTN AnimeList
 * @author: HammerOnTheNail
 * @link: http://code.google.com/p/hotnanimelist/
 */

// admin felületen a project elmentő és modosító törlő rendszer, csak mert lusta vagyok ctrl-c-zni
class hotn_animelist_admin_project
{
	function hotn_animelist_admin_project()
	{
		return TRUE;
	}
	
	// Egy project adat lekérdezése
	function hotn_animelist_admin_project_get_one($project_id)
	{
		global $sql;
		$sql->db_Select("hotn_animelist_project","*","`project_id` = '".$project_id."'");
		return $sql->db_Fetch();
	}
	
	function hotn_animelist_admin_project_save($type,$data)
	{
		global $sql, $ns, $HOTN_ANIMELIST_FORUM, $HOTN_ANIMELIST_DOWNLOAD;
		
		// küldött elemek kimentése
		$genre = $data['genre'];
		$project_id = $data['project_id'];
		
		// adatok ellenőrzése
			$error = FALSE;
			// nincs kejelölve project típus
			if ($data['project_type'] == "none")
			{
				$error[] = HOTN_ANIMELIST_ADMIN_PROJECT_ERROR_PROJECT_TYPE; 
			}
			
			// nincs megadva project mappa
			if ($data['project_folder'] == "")
			{
				$error[] = HOTN_ANIMELIST_ADMIN_PROJECT_ERROR_NO_PROJECT_NAME; 
			}
			// hibásan van megadva a project mappa
			elseif (ereg("[^A-Za-z0-9-_ ]",$data['project_folder']))
			{
				$error[] = HOTN_ANIMELIST_ADMIN_PROJECT_ERROR_AN_PROJECT_NAME; 	
			}

			// nincs megadva project név
			if ($data['project_name'] == "")
			{
				$error[] = HOTN_ANIMELIST_ADMIN_PROJECT_ERROR_PROJECT_TITLE; 
			}
			
			// nincs megadva project epizód szám
			if ($data['project_length'] == "")
			{
				$error[] = HOTN_ANIMELIST_ADMIN_PROJECT_ERROR_NO_PROJECT_EPISODE; 
			}
			// nem számokkal van megadva
			elseif (ereg("[^0-9]",$data['project_anime_episode']))
			{
				$error[] = HOTN_ANIMELIST_ADMIN_PROJECT_ERROR_NUM_PROJECT_EPISODE; 
				$error = TRUE;
			}
			
			// nincs megadva anime típus
			if($data['project_anime_type'] == "none" && $data['project_type'] <> "manga")
			{
				$error[] = HOTN_ANIMELIST_ADMIN_PROJECT_ERROR_PROJECT_ANIME_TYPE; 
			}
			
			if ($error!==FALSE)
			{
				// Hibásan van valameik adat megadva
				$values = $_POST;
				$themp_array = array(
					"genre",
					"project_translator",
					"project_karaoke",
					"project_format",
					"project_manga_format",
					"project_video",
					"project_lector",
					"project_timer"
				);
				foreach($themp_array as $themp_array)
				{
					$themp = array();
					foreach($values[$themp_array] as $id)
					{
						$themp[$id] = 1;
					}
					$values[$themp_array] = $themp;
				}
				
				$ns -> tablerender(HOTN_ANIMELIST_ADMIN_DEFAULT_ERROR_TITLE, implode("<br/>",$error));
				
				return $values;
			}
			else
			{
				// minden adat megfelelő, mehet a mentés! \o/
					// Mappák létrehozása
					@mkdir(e_DOWNLOAD.$data['project_folder']);
					//@chmod(e_DOWNLOAD.$data['project_folder'], 0775);
					@mkdir(e_DOWNLOAD.$data['project_folder']."/anime");
					//@chmod(e_DOWNLOAD.$data['project_folder']."/anime", 0775);
					@mkdir(e_DOWNLOAD.$data['project_folder']."/manga");
					//@chmod(e_DOWNLOAD.$data['project_folder']."/manga", 0775);
					@mkdir(e_DOWNLOAD.$data['project_folder']."/subtitle");
					//@chmod(e_DOWNLOAD.$data['project_folder']."/subtitle", 0775);
					@mkdir(e_PLUGIN."hotn_animelist/Project/".$data['project_folder']);
					//@chmod(e_PLUGIN."hotn_animelist/Project/".$data['project_folder'], 0775);
					
				// Adatok mentése
					if ($data['project_mode'] == "finished") $data['project_date_end'] = time();
					
					$themp_array = array(
						"project_translator",
						"project_karaoke",
						"project_format",
						"project_manga_format",
						"project_video",
						"project_lector",
						"project_timer"
					);
					
					foreach($themp_array as $themp_array)
					{
						if (!isset($data[$themp_array]))
							$data[$themp_array] = "";
						else
							$data[$themp_array] = implode("%",$data[$themp_array]);
					}
					
					if ($type=="new")
					{
						// Új project első mentse
						$data['project_date_start'] = time();
						
						// Fórum létrehozása
						$data['project_forum'] = $HOTN_ANIMELIST_FORUM->hotn_animelist_forum_create($data['project_name'],$data['project_forum_text'],$data['project_mode']);
						// Letöltés létrehozása
						$download = $HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_create($data['project_name']);
						
						$data['project_download_manga'] = $download['manga'];
						$data['project_download_anime'] = $downloag['anime'];
						$data['project_download_subtitle'] = $download['subtitle'];
						
						// nem szükséges adatok eldobása
						unset($data['action']);
						unset($data['project_submit']);
						unset($data['genre']);
						unset($data['project_forum_text']);
						unset($data['help']);
						unset($data['preimageselect']);
						unset($data['project_id']);
						unset($data['sizeselect']);
		
						$project_id = $sql -> db_Insert("hotn_animelist_project",$data);
					}
					else
					{
						// Már meglévő project elmentése
						// nem szükséges adatok eldobása
						unset($data['action']);
						unset($data['project_submit']);
						unset($data['genre']);
						unset($data['project_forum_text']);
						unset($data['help']);
						unset($data['preimageselect']);
						unset($data['project_id']);
						unset($data['sizeselect']);
						
						foreach ($data as $name => $value)
						{
							$update_arg[] = "`".$name."` = '".$value."'";
						}
						
						$update_arg = implode(", ",$update_arg);
						$sql->db_Update("hotn_animelist_project",$update_arg." WHERE `project_id` = '".$project_id."'");

					}						
					
				// Kategóriák hozzáadása
					if ($type=="edit")
					{
						$sql->db_Delete("hotn_animelist_genre","project_id = '".$project_id."'");
					}
					
					foreach ($genre as $genre)
					{
						$sql -> db_Insert("hotn_animelist_genre",array("project_id" => $project_id, "genre_id" => $genre));
					}
					
				// Képek feltöltése
					$upload_arg = "";
			
					require_once(e_HANDLER."upload_handler.php");
					require_once(e_HANDLER."resize_handler.php");
					$uploaded = process_uploaded_files(e_PLUGIN."hotn_animelist/Project/".$data['project_folder'], "unique", array('file_array_name'=>"project_image"));
					//-----
					
					// Képek átméretezése
						$image_arg = array();
						$error = FALSE;
						// Minden kép megvizsgálása
						foreach ($uploaded as $upload_id => $upload_array)
						{
							switch ($upload_array['index'])
							{
								case 0:
									if ($upload_array['error'] == 0)
									{
										$image_arg["project_image_reference"] = $upload_array['name'];
										resize_image(e_PLUGIN."hotn_animelist/Project/".$data['project_folder']."/".$upload_array['name'], e_PLUGIN."hotn_animelist/Project/".$data['project_folder']."/".$upload_array['name'], 220,"upsize");
									}
									else
									{
										$error[] = $upload_array['message'];
									}
									break;
								case 1:
									if ($upload_array['error'] == 0)
									{
										$image_arg["project_image_news"] = $upload_array['name'];
									}
									else
									{
										$error[] = $upload_array['message'];
									} 
									break;
								case 2:
								case 3:
								case 4:
								case 5:
								case 6:
								case 7:
									if ($upload_array['error'] == 0)
									{
										$image_arg["project_image_".($upload_array['index']-1)] = $upload_array['name'];
										resize_image(e_PLUGIN."hotn_animelist/Project/".$data['project_folder']."/".$upload_array['name'], e_PLUGIN."hotn_animelist/Project/".$data['project_folder']."/thumb_".$upload_array['name'], 150,"upsize");
									}
									else
									{
										$error[] = $upload_array['message'];
									}
									break;
								default:
										// nemcsinálunk semmit - ez az ág elvileg sosem fut le!
										print "Gratulálok! oan hibát okoztál, aminek sosem szabadott volna bekövetkeznie!";
									break;
							}
						}
						
						// Hibák ellenőrzése, van hiba?
						if ($error !== FALSE)
						{
							$ns -> tablerender(HOTN_ANIMELIST_ADMIN_DEFAULT_ERROR_TITLE, implode("<br/>",$error));
						}
						
						foreach ($image_arg as $image_id => $image_arg)
						{
							$upload_arg[] = "`".$image_id."` = '".$image_arg."'";
						}
					
				// Utolsó simitások elvégzése
					$upload_arg = implode(", ",$upload_arg);
					if ($upload_arg <> "") $sql -> db_Update("hotn_animelist_project",$upload_arg." WHERE project_id = '".$project_id."'");
					
				return TRUE;
			}	
	}

}

$HOTN_ANIMELIST_ADMIN_PROJECT = new hotn_animelist_admin_project();
?>